<template>
  <div class="login">
    <div class="loginCard">
      <img src="../../assets/logo.595745bd.png" alt="">
      <el-form
        ref="loginRef"
        :model="loginForm"
        class="demo-ruleForm"
        :rules="LoginRules"
      >
        <!-- 账号 -->
        <el-form-item label="" prop="loginName" autocomplete="on">
          <el-input
            v-model="loginForm.loginName"
            type="mobile"
            placeholder="请输入账号"
            prefix-icon="el-icon-mobile-phone"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            class="input"
            suffix-icon="el-icon-view"
          />
        </el-form-item>
        <!-- http://likede2-admin.itheima.net/likede/ -->
        <!-- 验证码 -->
        <el-form-item label="" prop="code">
          <el-input
            v-model="loginForm.code"
            type="text"
            placeholder="请输入验证码"
            prefix-icon="el-icon-circle-check"
          />
          <div class="verification">
            <img
              :src="`https://likede2-java.itheima.net/api/user-service/user/imageCode/${loginForm.clientToken}`"
              alt=""
            >
          </div>
        </el-form-item>
        <el-button type="button" @click="aa">登录</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
import { loginAPI } from '@/api'
export default {
  data() {
    return {
      LoginRules: {
        loginForm: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      // 登录 表单数据对象 在model上绑定
      loginForm: {
        // 账号
        loginName: 'admin',
        // 密码
        password: 'admin',
        // 验证码
        code: '',
        // 客户端请求验证码的token
        clientToken: Math.random().toString(36).substring(2),
        loginType: 0
      },
      loginToken: ''
    }
  },
  computed: {},
  created() {},
  methods: {
    random() {
      this.loginForm.clientToken = Math.random().toString(36).substring(2)
      console.log(this.loginForm.clientToken)
    },
    async aa() {
      this.$refs.loginRef.validate(async(valid) => {
        if (!valid) return
        const res = await loginAPI(this.loginForm)
        // console.log(res)
        window.localStorage.setItem('loginName', res.userName)
        window.localStorage.setItem('token', res.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}
.login {
  background-image: url("../../assets/background.be4fae7d.png");
  background-size: cover;
  width: 100%;
  height: 100%;
  position: relative;
  .loginCard {
    width: 518px;
    height: 388px;
    background-color: #fff;
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 10px;
    img {
      width: 96px;
      height: 96px;
      position: absolute;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .el-form {
      margin: 0 auto;
      margin-top: 100px;
      width: 80%;
      .el-form-item {
        width: 100%;
        height: 52px !important;
        background: #fff;
        border-radius: 4px;
        border: none;
        color: #999;
      }
      .verification {
        width: 135px;
        height: 52px;
        border-bottom-right-radius: 4px;
        border-top-right-radius: 4px;
        display: inline;
        img {
          width: 135px;
          height: 40px;
          top: 0;
          left: 327px;
          border-bottom-right-radius: 4px;
          border-top-right-radius: 4px;
        }
      }
      .el-button {
        width: 100%;
        height: 52px;
        background: linear-gradient(262deg, #2e50e1, #6878f0);
        opacity: 0.91;
        border-radius: 8px;
        color: #fff;
        text-shadow: 0 7px 22px #cfcfcf;
      }
    }
  }
}
</style>
